//
//  MainViewController.m
//  tcpoctest
//
//  Created by gump on 10/21/12.
//  Copyright (c) 2012 gump. All rights reserved.
//

#import "MainViewController.h"

#import "OnlinePeopleDataSource.h"
#import "sendOnlineP.h"
#import "taskDataSource.h"
#import "OnlinePeopleView.h"

@interface MainViewController ()

@end

@implementation MainViewController

-(id) init
{
	if ((self = [super init]) != nil)
	{
        [self initViewController];
    }
    
    return self;
}

-(void)loadView
{
    [super loadView];
}

#pragma mark -private fun
-(void)initViewController
{
    NSMutableArray* viewCtrls = [[NSMutableArray alloc] init];
    
    [viewCtrls addObject:[ViewManager sharedViewManager].onlinepeoplevc];
    [viewCtrls addObject:[ViewManager sharedViewManager].xxvc];
    [viewCtrls addObject:[ViewManager sharedViewManager].firendvc];
    [viewCtrls addObject:[ViewManager sharedViewManager].servervc];
    [viewCtrls addObject:[ViewManager sharedViewManager].setvc];
    
    self.viewControllers = viewCtrls;
    viewCtrls = nil;
    
    
    UIImage* imgpp = [UIImage imageNamed:@"tab_online.png"];
    [(UITabBarItem*)[self.tabBar.items objectAtIndex:0] setImage:imgpp];
    imgpp = [UIImage imageNamed:@"tabarpp.png"];
    [(UITabBarItem*)[self.tabBar.items objectAtIndex:1] setImage:imgpp];
    imgpp = [UIImage imageNamed:@"tabfirend.png"];
    [(UITabBarItem*)[self.tabBar.items objectAtIndex:2] setImage:imgpp];
    imgpp = [UIImage imageNamed:@"tabserver.png"];
    [(UITabBarItem*)[self.tabBar.items objectAtIndex:3] setImage:imgpp];
    imgpp = [UIImage imageNamed:@"tabset.png"];
    [(UITabBarItem*)[self.tabBar.items objectAtIndex:4] setImage:imgpp];
    
    
    [(UITabBarItem*)[self.tabBar.items objectAtIndex:0] setTitle:@"在线"];
    [(UITabBarItem*)[self.tabBar.items objectAtIndex:0] setTag:0];
    [(UITabBarItem*)[self.tabBar.items objectAtIndex:1] setTitle:@"消息"];
    [(UITabBarItem*)[self.tabBar.items objectAtIndex:1] setTag:1];
    [(UITabBarItem*)[self.tabBar.items objectAtIndex:2] setTitle:@"朋友"];
    [(UITabBarItem*)[self.tabBar.items objectAtIndex:2] setTag:2];
    [(UITabBarItem*)[self.tabBar.items objectAtIndex:3] setTitle:@"服务"];
    [(UITabBarItem*)[self.tabBar.items objectAtIndex:3] setTag:3];
    [(UITabBarItem*)[self.tabBar.items objectAtIndex:4] setTitle:@"设置"];
    [(UITabBarItem*)[self.tabBar.items objectAtIndex:4] setTag:4];
    
    [self setSelectedIndex:0];
}

#pragma mark -private fun-
-(void)setXMMessageNum:(int)num
{
    if (num > 0)
    {
        [(UITabBarItem*)[self.tabBar.items objectAtIndex:1] setBadgeValue:[NSString stringWithFormat:@"%d",num]];
    }
    else
    {
        [DataControl shareControl].mBadgeCount = 0;
        [(UITabBarItem*)[self.tabBar.items objectAtIndex:1] setBadgeValue:nil];
    }
}

-(void)addXMMNum
{
    [DataControl shareControl].mBadgeCount += 1;
    [self setXMMessageNum:[DataControl shareControl].mBadgeCount];
}

-(void)subXMMNum
{
    [DataControl shareControl].mBadgeCount -= 1;
    [self setXMMessageNum:[DataControl shareControl].mBadgeCount];
}

-(void)ShowFirCirBadge
{
    [(UITabBarItem*)[self.tabBar.items objectAtIndex:3] setBadgeValue:@"new"];
}

-(void)HideFirCirBadge
{
    [(UITabBarItem*)[self.tabBar.items objectAtIndex:3] setBadgeValue:nil];
}

#pragma mark -tabbar delegate-
-(void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item
{
    switch (item.tag) {
        case 0:
        {
            [[ViewManager sharedViewManager] insertRootView:
             (CommonViewController *)[ViewManager sharedViewManager].onlinepeoplevc];
            
            [[ViewManager sharedViewManager].onlinepeoplevc viewWillShow];
            
            if ([[DataControl shareControl].OnlinePeoDS GetOnlinePeopleOrder] == OLOT_MALE) {
                [[netManager sharedNetManager].sendonline sendonline:1];
            }
            else
            {
                [[netManager sharedNetManager].sendonline sendonline:2];
            }
            
            
        }
            break;
        case 1:
        {
            [[ViewManager sharedViewManager] insertRootView:
            (CommonViewController *)[ViewManager sharedViewManager].xxvc];
        }
            break;
        case 2:
        {
            [[ViewManager sharedViewManager] insertRootView:
            (CommonViewController *)[ViewManager sharedViewManager].firendvc];
        }
            break;
        case 3:
        {
            [[ViewManager sharedViewManager] insertRootView:
            (CommonViewController *)[ViewManager sharedViewManager].servervc];
        }
            break;
        case 4:
        {
            [[ViewManager sharedViewManager] insertRootView:
            (CommonViewController *)[ViewManager sharedViewManager].setvc];
        }
            break;
            
        default:
            break;
    }
}

@end
